package com.bluefuture.common.utils;

import org.apache.commons.lang.StringUtils;

import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期处理
 *
 * @author Mark sunlightcs@gmail.com
 */
public class DateUtils {
    /**
     * 时间格式(yyyy-MM-dd)
     */
    public final static String DATE_PATTERN = "yyyy-MM-dd";
    /**
     * 时间格式(yyyy-MM-dd HH:mm:ss)
     */
    public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

    /**
     * 日期格式化 日期格式为：yyyy-MM-dd HH:mm:ss
     *
     * @param date 日期
     * @return 返回yyyy-MM-dd HH:mm:ss格式日期
     */
    public static String format(LocalDateTime date) {
        return format(date, DATE_TIME_PATTERN);
    }

    public static Integer getBetweenDays(Date startTime, Date endTime) {
        int betweenDays = 0;
        long start = startTime.getTime();
        long end = endTime.getTime();

        betweenDays = (int) (Math.abs(end - start)/(24*3600*1000));

        return betweenDays + 1;
    }

    /**
     * 日期格式化
     *
     * @param date    日期
     * @param pattern 格式，如：DateUtils.DATE_TIME_PATTERN
     * @return 返回yyyy-MM-dd格式日期
     */
    public static String format(LocalDateTime date, String pattern) {
        if (date != null) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
            return formatter.format(date);
        }
        return null;
    }

    /**
     * 日期解析
     *
     * @param date    日期
     * @param pattern 格式，如：DateUtils.DATE_TIME_PATTERN
     * @return 返回Date
     */
    public static LocalDateTime parse(String date, String pattern) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);

        LocalDateTime localDateTime = LocalDateTime.parse(date, formatter);
        return localDateTime;
    }

    /**
     * 字符串转换成日期
     *
     * @param strDate 日期字符串
     * @param pattern 日期的格式，如：DateUtils.DATE_TIME_PATTERN
     */
    public static LocalDateTime stringToLocalDateTime(String strDate, String pattern) {
        if (StringUtils.isBlank(strDate)) {
            return null;
        }

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        LocalDateTime localDateTime = LocalDateTime.parse(strDate, formatter);
        return localDateTime;
    }

    /**
     * 时间戳获取
     *
     * @param localDateTime
     * @return 返回时间戳
     */
    public static Long timestamp(LocalDateTime localDateTime) {
        return localDateTime.toEpochSecond(ZoneOffset.of("+8"));
    }

    /**
     * 根据周数，获取开始日期、结束日期
     *
     * @param week 周期  0本周，-1上周，-2上上周，1下周，2下下周
     * @return 返回date[0]开始日期、date[1]结束日期
     */
    public static LocalDateTime[] getWeekStartAndEnd(long week) {
        LocalDateTime beginDate = LocalDateTime.of(LocalDate.now(), LocalTime.MIN).with(DayOfWeek.MONDAY);
        beginDate.plusWeeks(week);

        LocalDateTime endDate = beginDate.plusDays(6);
        return new LocalDateTime[]{beginDate, endDate};
    }

    /**
     * 对日期的【秒】进行加/减
     *
     * @param localDateTime 日期
     * @param seconds       秒数，负数为减
     * @return 加/减几秒后的日期
     */
    public static LocalDateTime addDateSeconds(LocalDateTime localDateTime, long seconds) {
        return localDateTime.plusSeconds(seconds);
    }

    /**
     * 对日期的【分钟】进行加/减
     *
     * @param localDateTime 日期
     * @param minutes       分钟数，负数为减
     * @return 加/减几分钟后的日期
     */
    public static LocalDateTime addDateMinutes(LocalDateTime localDateTime, long minutes) {
        return localDateTime.plusMinutes(minutes);
    }

    /**
     * 对日期的【小时】进行加/减
     *
     * @param localDateTime 日期
     * @param hours         小时数，负数为减
     * @return 加/减几小时后的日期
     */
    public static LocalDateTime addDateHours(LocalDateTime localDateTime, long hours) {
        return localDateTime.plusHours(hours);
    }

    /**
     * 对日期的【天】进行加/减
     *
     * @param localDateTime 日期
     * @param days          天数，负数为减
     * @return 加/减几天后的日期
     */
    public static LocalDateTime addDateDays(LocalDateTime localDateTime, long days) {
        return localDateTime.plusDays(days);
    }

    /**
     * 对日期的【周】进行加/减
     *
     * @param localDateTime 日期
     * @param weeks         周数，负数为减
     * @return 加/减几周后的日期
     */
    public static LocalDateTime addDateWeeks(LocalDateTime localDateTime, long weeks) {
        return localDateTime.plusWeeks(weeks);
    }

    /**
     * 对日期的【月】进行加/减
     *
     * @param localDateTime 日期
     * @param months        月数，负数为减
     * @return 加/减几月后的日期
     */
    public static LocalDateTime addDateMonths(LocalDateTime localDateTime, long months) {
        return localDateTime.plusMonths(months);
    }

    /**
     * 对日期的【年】进行加/减
     *
     * @param localDateTime 日期
     * @param years         年数，负数为减
     * @return 加/减几年后的日期
     */
    public static LocalDateTime addDateYears(LocalDateTime localDateTime, long years) {
        return localDateTime.plusYears(years);
    }

    /**
     * 获取今天的起始时间
     *
     * @return
     */
    public static Date getDayBeginDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 获取今天的结束时间
     *
     * @return
     */
    public static Date getDayEndDate() {
        Date today = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(today);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    public static void main(String[] args) throws InterruptedException {
        while(true){
            Thread.sleep(200);
            Date dayEndDate = getDayEndDate();
            Calendar instance = Calendar.getInstance();
            instance.setTime(dayEndDate);
            int i = instance.get(Calendar.DAY_OF_MONTH);
            if (i==28){
                System.out.println(dayEndDate);
            }
        }
//        System.out.println(Calendar.getInstance().hashCode());
//        System.out.println(Calendar.getInstance().hashCode());
//        System.out.println(getDayBeginDate());
//        System.out.println(getDayEndDate());
//        System.out.println(getWeekFirstDate());
//        System.out.println(getWeekLastDate());
//        System.out.println(getMonthFirstDate());
//        System.out.println(getMonthLastDate());
    }

    /**
     * 格式化
     *
     * @param date
     * @param pattern
     * @return
     */
    public static String format(Date date, String pattern) {
        if (date != null) {
            SimpleDateFormat formatter = new SimpleDateFormat(pattern);
            return formatter.format(date);
        }
        return "";
    }

    /**
     * 获取本月第一天的零点时间
     *
     * @return
     */
    public static Date getMonthFirstDate() {
        //获取当前月第一天：
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 获取本星期一的零点时间
     *
     * @return
     */
    public static Date getWeekFirstDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(getThisWeekMonday(new Date()));
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }
    /**
     * 获取本星期日的59点59分时间
     *
     * @return
     */
    public static Date getWeekLastDate() {
        //获取当前月第一天：
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(getSundayOfThisWeek());
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 获取本月最后一天的59点59分的时间
     *
     * @return
     */
    public static Date getMonthLastDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    public static Date getThisWeekMonday(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        // 获得当前日期是一个星期的第几天
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        // 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        // 获得当前日期是一个星期的第几天
        int day = cal.get(Calendar.DAY_OF_WEEK);
        // 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
        return cal.getTime();
    }

    public static Date getSundayOfThisWeek() {
        Calendar c = Calendar.getInstance();
        int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;
        if (day_of_week == 0) {
            day_of_week = 7;
        }
        c.add(Calendar.DATE, -day_of_week + 7);
        return c.getTime();
    }


}
